# -*- encoding: utf-8 -*-
##############################################################################
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see http://www.gnu.org/licenses/.
#
##############################################################################

from odoo import models, fields, api
from odoo.tools.translate import _
from odoo.exceptions import ValidationError
from odoo.tools.float_utils import float_round


class stock_quant(models.Model):
    _inherit = 'stock.quant'

    lot_uom = fields.Many2one('product.uom', string=u'辅助单位', related='lot_id.lot_uom')
    lot_uom_qty = fields.Float(u'辅助单位数量', compute='_compute_lot_uom_qty')
    lot_uom_ratio = fields.Float(u'换算率', related='lot_id.lot_uom_ratio', digits=(16, 4))
    lot_lenth = fields.Char(u'全长', related='lot_id.lot_lenth')
    code = fields.Char(u'代码', related='lot_id.code')
    description = fields.Char(u'说明', related='lot_id.description')
    board_number = fields.Char(u'内部参考', related='lot_id.board_number')

    @api.depends('lot_uom_ratio')
    def _compute_lot_uom_qty(self):
        for quant in self:
            if quant.lot_uom_ratio > 0:
                quant.lot_uom_qty = quant.qty / quant.lot_uom_ratio
